#include <stdio.h>
#include <string.h>

void reverse(char*, int, int);
void left_rotate(char*, int);
void right_rotate(char*, int);

/**
 * 字符串左旋，右旋
*/
int main(int argc, char const *argv[])
{
    char *str = "welcome";
    left_rotate(str, 3);
    printf("%s\n", str);

    return 0;
}

void reverse(char *str, int start, int end)
{
    while (start < end)
    {
        char temp = str[start];
        str[start] = str[end];
        str[end] = temp;
        start++;
        end--;
    }
}

void left_rotate(char *str, int k)
{
    int len = strlen(str);
    k = k % len;
    reverse(str, 0, k - 1);
    reverse(str, k, len - 1);
    reverse(str, 0, len - 1);
}

void right_rotate(char *str, int k)
{
    int len = strlen(str);
    k = k % len;
    reverse(str, 0, len - 1);
    reverse(str, 0, k - 1);
    reverse(str, k, len - 1);
}